﻿ using Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Cake_Management.Controllers
{
    public class HT_CategoryController : Controller
    {
        // GET: HT_Category
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetAll()
        {
            var result = new BLL.B_Category().GetAll();
            var content = from c in result
                          select new
                          {
                              c.Id,
                              c.CategoryName,
                              LsView = c.LsView? "已启用" : "禁用",
                              cc = $"<div class='btn btn-warning data-Edit'data-id='{c.Id}'onclick='Edit(this)'>编辑</div>   <div class='btn btn-danger data-del' data-id='{c.Id}' onclick='Delete(this)'>删除</div>"
                          };
            var data = new { data = content };
            return Json(data,JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult Add()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult Add(Category category)
        {
            new BLL.B_Category().Add(category);
            return Json(new { Code = 200,Message="添加成功"});
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var data=new BLL.B_Category().FindById(id);
            return PartialView(data);
        }

        [HttpPost]
        public ActionResult Edit(Category category)
        {
            new BLL.B_Category().Updata(category.Id, category);
            return Json(new { Code = 200, Message = "修改成功" });
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                new BLL.B_Category().Delete(id);
                return Json(new { Code = 200, Message = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { Code = 500, Message = "删除失败，请先删除栏目下的所有商品：" + ex.Message });                
            }
        }

    }
}